/*
this program is used to send msg to trigger the ele machine operation
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include "../app/msg_def.h"
#include "../app/config/config.h"

#define FILE_NAME "module.txt"


int main(int argc, char *argv[])
{
    FILE * fp;
    unsigned char sid = 0;
    unsigned char tcw = 0;
    int flag_file = 0;
    int dlc_f = 0;
    unsigned char data_f[8];

    struct can_frame canFrame;

    module_queue_msg ctlFrame;

    memset(&ctlFrame,0,sizeof(module_queue_msg));

    int flagValid = 0;
    ctlFrame.moduleMsg.moduleId = -1;
    if(argc<2)
    {
        fp = fopen(FILE_NAME,"r");       

    }
    else
    {
        fp = fopen(argv[1],"r");
    }

    if(fp!=NULL)
    {
        char StrLine[256];
        char * tmpPtr = NULL;
        while(!feof(fp))
        {
           memset(StrLine,0,256);
           fgets(StrLine,256,fp);
           if(strstr(StrLine,"name"))
           {
               tmpPtr = StrLine+5;
               //printf("string is %s,substring is %s, std string is %s\n",StrLine,tmpPtr,"closelid");
               if(strstr(tmpPtr,"openlid"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_OPENLID;
               }
               if(strstr(tmpPtr,"closelid"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_CLOSELID;
               }

               if(strstr(tmpPtr,"xr"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_XR;
               }

               if(strstr(tmpPtr,"bash"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_BATH;
               }
               
               if(strstr(tmpPtr,"trans"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_TRANS;
               }

               if(strstr(tmpPtr,"bamove"))
               {
                   ctlFrame.moduleMsg.moduleId = I_MODULE_BASHMOVE;
               }

               if(strstr(tmpPtr,"bazmove"))
               {
                   ctlFrame.moduleMsg.moduleId = I_MODULE_BATHZMOVE;
               }

               if(strstr(tmpPtr,"dp8move"))
               {
                   ctlFrame.moduleMsg.moduleId = I_FILE_DP8;
               }

               if(strstr(tmpPtr,"dp8suck"))
               {
                   ctlFrame.moduleMsg.moduleId = I_MODULE_DP8SUCK;
               }

                if(strstr(tmpPtr,"dp8droptip"))
                {
                    ctlFrame.moduleMsg.moduleId = I_MODULE_DP8DROPTIP;
                }

                if(strstr(tmpPtr,"dp8spit"))
                {
                    ctlFrame.moduleMsg.moduleId = I_MODULE_DP8SPIT;
                }

                if(strstr(tmpPtr,"dp8ontip"))
                {
                    ctlFrame.moduleMsg.moduleId = I_MODULE_DP8ONTIP;
                }

                if(strstr(tmpPtr,"djclean"))
                {
                    ctlFrame.moduleMsg.moduleId = I_MODULE_DJLIQUID;
                }
                


            //    else
            //    {
            //        printf("compare ret is %d\n",ret);
            //        /* code */
            //    }
               
           }
           if(strstr(StrLine,"sid"))
            {
                tmpPtr = StrLine+4;
                sid = atoi(tmpPtr);
                flag_file = 1;
                ctlFrame.moduleMsg.un_param.moveParam.sid = sid;
            }

            if(strstr(StrLine,"pos"))
            {
                int tmpDis = atoi(StrLine+4);
                ctlFrame.moduleMsg.un_param.moveParam.pos = tmpDis;
                flagValid |= 0x10;
            }     

            if(strstr(StrLine,"inc_speed"))
            {
                int tmpNum = atoi(StrLine+10);
                ctlFrame.moduleMsg.un_param.moveParam.inc_speed = tmpNum;
                continue;
                //flagValid |= 0x20;
            }
            if(strstr(StrLine,"dec_speed"))
            {
                int tmpNum = atoi(StrLine+10);
                ctlFrame.moduleMsg.un_param.moveParam.dec_speed = tmpNum;
                continue;
                //flagValid |= 0x20;
            }

            if(strstr(StrLine,"speed"))
            {
                int tmpNum = atoi(StrLine+6);
                ctlFrame.moduleMsg.un_param.moveParam.speed = tmpNum;
                continue;
                //flagValid |= 0x20;
            }         

        }

        fclose(fp);
        // char strPrint[128];
        // int n=0, n1=0;
        // memset(strPrint,0,128);
        // if(dlc_f)
        // {
        //    n = snprintf(strPrint,128,"%02x ",data_f[0]);
        //    for(int i=1;i<dlc_f;i++)
        //    {
        //        n1 = snprintf(strPrint+n,128-n,"%02x ",data_f[i]);
        //        n += n1;
        //    }
        //    printf("data:%s\n",strPrint);        

        // }
        
    }
    else
    {
        /* code */
        printf("open file failed\n");
    }

    // if(ctlFrame.moduleMsg.moduleId == -1)
    // {
    //     return -1;
    // }

    key_t key = ftok(msgQueueKeyPath, 0x1234);
    if(key < 0)
    {
        perror("ftok");
        printf("%s\n",msgQueueKeyPath);
        return -1;
    }

    int msg_id = msgget(key, IPC_CREAT|0600);//IPC_CREAT|0600
    if(msg_id < 0)
    {
        perror("msgget");
        return -1;
    }

    //memcpy(canFrame.data,data_f,canFrame.can_dlc);

    
    // ctlFrame.type = 1;
    // ctlFrame.ctlFrame.opcode = MOVE;
    // ctlFrame.ctlFrame.sid = sid;
    // ctlFrame.ctlFrame.dir = 1;
    // ctlFrame.ctlFrame.position = 2000;
    //memcpy(msgFrame.mtext,&canFrame,sizeof(struct can_frame));

    //if(msgsnd(msg_id, (void*)&msgFrame, sizeof(struct can_frame), 0) < 0)
    //{
    //    perror("msgsnd");
    //    return -1;
    //}
    
    ctlFrame.type = QUEUE_MSG_TYPE_MODULE;
    if(msgsnd(msg_id, (void*)&ctlFrame, sizeof(module_msg), 0) < 0)
    {
        perror("msgsnd");
        return -1;
    }
    else
    {
        /* code */
        printf("msg send success,type is %d,moduleId[%d]\n",ctlFrame.type,ctlFrame.moduleMsg.moduleId);
    }
         
    
    //msgctl(msg_id,IPC_RMID,NULL);

    return 0;
} 
/*
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>

#define TEXT_SIZE 512

struct _msgbuf
{

    long mtype;
    int status;
    char time[20];
    char mtext[TEXT_SIZE];    
};

char *getxtsj()
{
    time_t tv;
    struct tm *tmp;
    static char buf[20];

    tv = time( 0 );

    tmp = localtime(&tv);

    sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
    return buf;
}

int main(int argc, char **argv)

{

    int msqid ;

struct msqid_ds info;

struct _msgbuf buf;

struct _msgbuf buf1;

int flag ;

int sendlength, recvlength ;

int key ;



key = ftok("msg.tmp", 0x01 ) ;

if ( key < 0 )

{

perror("ftok key error") ;

return -1 ;

}



msqid = msgget( key, 0600|IPC_CREAT ) ;

if ( msqid < 0 )

{

 perror("create message queue error") ;
return -1 ;

}



buf.mtype = 1;

buf.status = 9;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "happy new year!\n") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}

buf.mtype = 3 ;

buf.status = 9 ;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "good bye!") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}
system("ipcs -q") ;

return 0 ;

}*/